
/**
 * 查询PLM研发项目数据库集合
 */

// 云开发初始化
const cloud = require('wx-server-sdk');
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV });
const db = cloud.database();

// 查询PLM研发项目集合数据
async function checkPlmResearchProjects() {
  try {
    console.log('开始查询PLM研发项目集合...');
    
    // 检查集合是否存在
    const collections = await db.listCollections().get();
    const collectionNames = collections.data.map(collection => collection.name);
    
    console.log('数据库集合列表:');
    collectionNames.forEach(name => console.log(`- ${name}`));
    
    // 检查PLM相关集合
    const plmCollections = collectionNames.filter(name => name.startsWith('plm_'));
    
    if (plmCollections.length === 0) {
      console.log('未找到PLM相关集合');
      return { code: -1, message: '未找到PLM相关集合' };
    }
    
    console.log('\nPLM相关集合:');
    plmCollections.forEach(name => console.log(`- ${name}`));
    
    // 查询plm_research_project集合数据
    if (collectionNames.includes('plm_research_project')) {
      const projects = await db.collection('plm_research_project').get();
      
      console.log('\nPLM研发项目数据:');
      console.log(JSON.stringify(projects.data, null, 2));
      
      return { 
        code: 0, 
        message: '查询成功', 
        data: projects.data 
      };
    } else {
      console.log('未找到plm_research_project集合');
      return { code: -1, message: '未找到plm_research_project集合' };
    }
  } catch (error) {
    console.error('查询失败:', error);
    return { code: -1, message: '查询失败: ' + error.message };
  }
}

// 执行查询
checkPlmResearchProjects().then(result => {
  console.log('\n查询结果:', result.message);
  process.exit(result.code === 0 ? 0 : 1);
});